<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组别ID" prop="groupid"> 
        <el-select v-model="queryParams.groupid" 
              clearable
            placeholder="请选择组别系统ID">  
            <el-option
              v-for="group in groupList"
              :key="group.id"
              :label="group.groupname"
              :value="group.id"
            ></el-option>
            </el-select>  
        
      </el-form-item>
      <el-form-item label="设备IP" prop="ip">
        <el-select v-model="queryParams.ip" 
              clearable
            placeholder="请选择设备IP地址">  
            <el-option
              v-for="device in deviceList"
              :key="device.deviceId"
              :label="device.deviceIp"
              :value="device.deviceIp"
            ></el-option>
            </el-select>
      </el-form-item> 
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['snmp:groupdevice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['snmp:groupdevice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['snmp:groupdevice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['snmp:groupdevice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupdeviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" /> 
       <el-table-column label="系统ID"  width="80" align="center" prop="deviceids" />
      <el-table-column label="组别ID"  width="80" align="center" prop="groupid" />
      <el-table-column label="分组名称"  width="120" align="center" prop="groupName" />
      
      <el-table-column label="设备ID"  width="120" align="center" prop="deviceId" />
      <el-table-column label="设备IP地址"  width="120" align="center" prop="groupip" />
      <el-table-column label="设备说明" align="center" prop="sysDecsr" />
      <el-table-column prop="description" label="分组说明"   width="120">  </el-table-column>
      <el-table-column
              prop="onlineStatus"
              label="状态"
              width="120">
            <template #default="scope">
              <span v-if="scope.row.onlineStatus === '在线'" class="st-square st-square-green">在线</span>
              <span v-else class="st-square st-square-red">未连接</span>
            </template>
          </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  v-if="false">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['snmp:groupdevice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['snmp:groupdevice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分组设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="组别ID" prop="groupid"> 
          <el-select v-model="form.groupid" 
            placeholder="请选择设备IP地址">  
            <el-option
              v-for="group in groupList"
              :key="group.id"
              :label="group.groupname"
              :value="group.id"
            ></el-option>
            </el-select> 
        </el-form-item>
        <el-form-item label="设备IP地址" prop="ip"> 
           <el-select v-model="form.ip" 
            placeholder="请选择设备IP地址">  
            <el-option
              v-for="device in deviceList"
              :key="device.deviceId"
              :label="device.deviceIp"
              :value="device.deviceId"
            ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="设备说明" prop="description">
          <el-input v-model="form.description" placeholder="请输入设备说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGroupdevice, listAllGroupdevice,getGroupdevice, delGroupdevice, addGroupdevice, updateGroupdevice } from "@/api/snmp/groupdevice";
import { allDeviceinfo } from "@/api/snmp/deviceinfo";
import { allistGroupinfo} from "@/api/snmp/groupinfo";

export default {
  name: "Groupdevice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      groupdeviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupid: null,
        ip: null,
        description: null
      },
      // 表单参数
      form: {},
      deviceList:[],
      groupList:[],
      // 表单校验
      rules: {
        groupid: [
          { required: true, message: "分组不能为空", trigger: "blur" }
        ],
        ip: [
          { required: true, message: "设备IP地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getdeviceList();
    this.getList();
  },
  methods: {
   /** 查询设备编号 */
   getdeviceList() {
    allDeviceinfo().then(response => {
        this.deviceList = response.data; 
      }); 
      allistGroupinfo().then(response => {
        this.groupList = response.data; 
      }); 
   },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listAllGroupdevice(this.queryParams).then(response => {
      debugger;
        this.loadRowData(response); 
      })
      .catch(error => {
        debugger;
       });
    },
    loadRowData(data){
      this.groupdeviceList = data.rows;
      this.total = data.total;
      this.loading = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        groupid: null,
        ip: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceids)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分组设备信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.deviceids || this.ids
      getGroupdevice(id).then(response => {
      debugger;
        this.form = response.data;
        this.open = true;
        this.title = "修改分组设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() { 
      this.$refs["form"].validate(valid => {
        if (valid) {
        debugger; 
          if (this.form.id != null) {
            updateGroupdevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroupdevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分组设备信息编号为"' + ids + '"的数据项？').then(function() {
        return delGroupdevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('snmp/groupdevice/export', {
        ...this.queryParams
      }, `groupdevice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">

.st-square-green {
  color:red;
  cursor: pointer;
  background-color: #3db58d;
}
.st-square-red {
  color:yellow;
  cursor: pointer;
  background-color: red;
}
</style>
